<?php

namespace App\Http\Controllers\Admin;

use App\Model\PushFlow;
use App\Model\PushFlowStart;
use App\Model\PushFlowEnd;
use App\Model\Video;
use App\Model\Users;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\Api\SendMessageRequest;
use App\Model\Message;
use App\Model\MessageText;
use DB;
use Illuminate\Support\Facades\Auth;
use phpDocumentor\Reflection\Types\Null_;
use App\Model\UserViedoWatchRule;


class VideoController extends Controller
{
    public function dataList(Request $request)
    {
        $type = $request->input('type');
        $videoName = $request->input('videoName');
        $query=Video::orderBy('id','desc');
        if($type){
            $query->where('type','=',$type);
        }else{
            $query->where('type','!=',1);
        }
        if($videoName){
            $query->where('videoName','like','%'.$videoName.'%');
        }
        $data = $query->paginate($request->get('limit',15))->toArray();
        //$get_status = Video::get_status_all();
        foreach ($data['data'] as $key=>$val){
            $data['data'][$key]['createTime'] = date('Y-m-d H:i:s',$val['createTime']);
            //$data['data'][$key]['type'] = $get_status[$val['type']]??'';
        }
        
        $data = [
            'code' => 200,
            'message'   => '请求成功',
            'data'  => $data['data'],
            'count'  => $data['total']
        ];
        return response()->json($data);
    }


    /**
     * 视频修改页面
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function referweb(Request $request)
    {
        //修改的话该值有值
        $id = isset($request['id']) ? $request['id'] : 0;
        $data = Video::where('id',$id)->first();
        if(empty($data)){
            $res_arr["code"] = 408;
            $res_arr["message"] = '该视频不存在，请联系管理员';
            return response()->json($res_arr);
        }
        $res_arr['data'] = $data;
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * 视频数据处理
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function videosdo(Request $request)
    {
        $redata = $request->all();
        $id = isset($redata['id']) ? $redata['id'] : 0;
        $videoName = isset($redata['videoName']) ? $redata['videoName'] : '';
        $data = Video::where('id',$id)->first();
        unset($redata['_token']);
        $name_data = Video::where('videoName',$videoName)->first();
        if(empty($videoName)){
            $res_arr["code"] = 408;
            $res_arr["message"] = '视频名称不能为空，请重新录入';
            return response()->json($res_arr);
        }
        if($data){
            if($name_data && $name_data['id']!=$id){
                $res_arr["code"] = 408;
                $res_arr["message"] = '该视频名称已存在，请重新录入';
                return response()->json($res_arr);
            }
            $res = Video::where('id', $id)->update($redata);
        }else{
            if($name_data){
                $res_arr["code"] = 408;
                $res_arr["message"] = '该视频名称已存在，请重新录入';
                return response()->json($res_arr);
            }
            unset($redata['id']);
            $user = Auth::user();
            $userName = $user->name;
            $redata['userName'] = $userName;
            $redata['createTime'] = time();
            $res = Video::insertGetId($redata);
        }
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * 视频数据删除
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function delete(Request $request)
    {
        $redata = $request->all();
        $id = isset($redata['id']) ? $redata['id'] : 0;
        $resu = Video::where('id', $id)->delete();
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * 生成推流地址
     * @param Request $quest
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function push(Request $request)
    {
        //更新直播状态跟直播服务商
        $time = time();
        //$streamName = 'live'.$video['course_id'].'video'.$video_id;  //流名称
        $streamName = 'live1video1';  //流名称
        $txTime = strtotime("+3 hour");  //地址有效期
        $key = 3895;
        $txSecret=md5($key.$streamName.$txTime);
        //$push_url = 'rtmp://aliyun-live-push.meedu.xyz/meedu/course43video70?auth_key=1653657019-jEjywW-0-bd489a67b2aedd8e948f21fe15ada67f';
        $redata['push_url'] = 'rtmp://120.224.176.28:1935/live/'.$streamName.'?txSecret='.$txSecret.'&txTime='.$txTime;

        /*$data = PushFlow::first();
        if($data){
            $res = PushFlow::update($redata);
        }else{
        }*/
        unset($redata['id']);
        $user = Auth::user();
        $userid = $user->id;
        $redata['user_id'] = $userid;
        $redata['created_at'] = date('Y-m-d H:i:s');
        $res = PushFlow::insertGetId($redata);

        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * 获取所有状态适用于下拉列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function status_all(Request $request)
    {
        $get_status = Video::get_status(); //状态
        $res_arr['data'] = $get_status;
        $res_arr["code"] = 200;
        $res_arr["message"] = 'success';
        return response()->json($res_arr);
    }

    /**
     * 直播列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function pushList(Request $request)
    {
        $query=PushFlow::orderBy('id','desc');
        $data = $query->paginate($request->get('limit',15))->toArray();
        $pushFlowEnd = PushFlowEnd::get()->toArray();
        $start = $end = [];
        foreach ($pushFlowEnd as $ke=>$ve){
            $end[$ve['push_flow_id']][$ve['user_id']] = $ve['user_id'];
        }
        $pushFlowStart = PushFlowStart::get()->toArray();
        foreach ($pushFlowStart as $ks=>$vs){
            if(isset($end[$vs['push_flow_id']][$vs['user_id']])){
                $start[$vs['push_flow_id']][$vs['user_id']] = $vs['user_id'];
            }
        }

        foreach ($data['data'] as $key=>$val){
            $count = 0;
            if (isset($start[$val['id']])){
                $count=count($start[$val['id']]);
            }
            $data['data'][$key]['count'] = $count;
        }

        $data = [
            'code' => 200,
            'message'   => '请求成功',
            'data'  => $data['data'],
            'count'  => $data['total']
        ];
        return response()->json($data);
    }

}
